/**
 * (C)2013 Exlu Corporation. All rights reserved.
 * 文件名 : cn.exlu.wea.tpk.test.SendFileMessage.java
 * 注意事項   :
 */
package cn.exlu.wea.tpk.test;

import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import cn.exlu.wea.tpk.email.LoginAuthentication;

/**
 * @author Mark 
 * @version 1.0.1
 * 2013-1-30
 * SendFileMessage.java
 */
public class SendFileMessage {

    /**
     * @param args
     * @throws MessagingException 
     * @throws AddressException 
     * @throws IOException 
     */
    public static void main(String[] args) throws AddressException, MessagingException, IOException {
        Properties prop = new Properties();
        prop.put("mail.smtp.host", "smtp.126.com");
        prop.put("mail.smtp.auth", true);
        prop.put("mail.debug", true);

        LoginAuthentication loginAuth = new LoginAuthentication("testlogin123@126.com",
            "testlogin1");
        Session session = Session.getDefaultInstance(prop, loginAuth);

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("testlogin123@126.com"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("mark@yikongxy.com"));
        message.setSubject("send a attachment to you");
        Multipart multipart = new MimeMultipart();
        BodyPart bodyPart = new MimeBodyPart();
        bodyPart.setText("Please open this is attachment");
        multipart.addBodyPart(bodyPart);
        //        bodyPart=new MimeBodyPart();
        //        DataSource dataSource=new FileDataSource("D:/HdReport.zip");
        //        bodyPart.setDataHandler(new DataHandler(dataSource));
        //        bodyPart.setFileName("HdReport.zip");
        //        multipart.addBodyPart(bodyPart);

        bodyPart = new MimeBodyPart();
        DataSource dataSource2 = new FileDataSource("D:/temp/DB_Data.xls");
        bodyPart.setDataHandler(new DataHandler(dataSource2));
        bodyPart.setFileName("Hatake Kakashi.xls");

        multipart.addBodyPart(bodyPart);

        message.setContent(multipart);
        //        Transport.send(message);
        Transport transport = session.getTransport("smtp");
        transport.connect();
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        //        Transport.send(message);
        //        Transport.send(message);
        System.out.println("Success");
    }

}
